package com.study.talk.ui.adapter.moment

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.study.talk.data.model.MomentComment
import com.study.talk.data.model.MomentLike
import com.study.talk.data.model.resp.MomentResponse

class MomentItemViewModel(val moment: MomentResponse) : ViewModel() {

    private val _likes = MutableLiveData<List<MomentLike>>()
    val likes: LiveData<List<MomentLike>> get() = _likes

    private val _comments = MutableLiveData<List<MomentComment>>()
    val comments: LiveData<List<MomentComment>> get() = _comments

    private val _photos = MutableLiveData<List<String>>()
    val photos : LiveData<List<String>> get() = _photos

    private val _myLikeState = MutableLiveData<Int>()
    val myLikeState : LiveData<Int> get() = _myLikeState

    init {
        _likes.postValue(moment.momentLikes)
        _comments.postValue(moment.momentComments)
        _photos.postValue(moment.photos)
        _myLikeState.postValue(moment.myLikeState)
    }

    fun changeLikes(likes : List<MomentLike>){
        _likes.postValue(likes)
    }

    fun changeComments(comments : List<MomentComment>){
        _comments.postValue(comments)
    }

    fun changeLikeState(state : Int){
        _myLikeState.postValue(state)
    }


    @Suppress("UNCHECKED_CAST")
    class Factory(private val moment: MomentResponse) : ViewModelProvider.Factory{
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(MomentItemViewModel::class.java)){
                return MomentItemViewModel(moment) as T
            }
            else throw IllegalArgumentException("Unknown ViewModel class")
        }
    }
}